//
//  ZZStatusPictureView.swift
//  sina
//
//  Created by 张鹏 on 16/5/28.
//  Copyright © 2016年 ZP. All rights reserved.
//显示微博配图的视图  数据要设置数据源代理  还要picture字典转模型拿到picture的值

import UIKit
//图片间距
 let pictureMargin :CGFloat = 3
//最大宽度
 let maxWidth = screenWidth - 2 * StatusCellMargin
//单张图片的宽度
 let itemWidth = (maxWidth - 2 * pictureMargin) / 3

//ID
let pictureCellID = "pictureCellID"

class ZZStatusPictureView: UICollectionView {
    
    //数据源
    var pictures : [ZZPicture]? {
    
        didSet{
            
            testLabel.text = "\(pictures?.count ?? 0)"
            print(testLabel.text)
            //根据图片的尺寸更新大小
            let pictureSize = calculatePictureViewSize()
    
            self.snp_updateConstraints { (make) in
                
                make.size.equalTo(pictureSize)
            }
            //刷新界面
            reloadData()
        }
    
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let layout = UICollectionViewFlowLayout()
        
        //设置间距
        layout.minimumInteritemSpacing = pictureMargin
        layout.minimumLineSpacing = pictureMargin
    
        super.init(frame: frame, collectionViewLayout: layout)
        
        //设置数据源代理
        self.dataSource = self
        self.backgroundColor = UIColor.whiteColor()
        
        //注册cell
        registerClass(ZZStatusPictureCell.self, forCellWithReuseIdentifier: pictureCellID)
        
//        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置UI
    
    private func setupUI() {
        addSubview(testLabel)
        //设置约束
        testLabel.snp_makeConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
    }
    
    private lazy var testLabel: UILabel = UILabel(title: "11", font: 30, textColor: UIColor.redColor())
    
    
    
    
    
    //根据图片的数量确定视图的大小
    func calculatePictureViewSize() -> CGSize {
    //设置图片的数量
    let imageCount = pictures?.count ?? 0
        
        //0 张
        if imageCount == 0 {
            return CGSizeZero
        }
        
        //1 张
        //获取collection的布局对象
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        if imageCount == 1 {
            //原图显示
            let imageSize = CGSize(width: 100, height: 150)
            
            layout.itemSize = imageSize
            
            return imageSize
        }
        
        //设置其他多张图片的itemSize
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        //4 张
        if imageCount == 4 {
            
            let width = itemWidth * 2 + pictureMargin
            return CGSize(width: width, height: width)
        }
        // 其他
        //1. 根据图片张数 计算 行数
        /*
         1,2,3   1
         4,5,6   2
         7,8,9   3
         */
        let row = CGFloat((imageCount - 1) / 3) + 1
        let height = row * itemWidth + (row - 1 ) * pictureMargin
        
        return CGSize(width: maxWidth, height: height)
    }

}

//数据源代理方法
extension ZZStatusPictureView : UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
      return pictures?.count ?? 0
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellID, forIndexPath: indexPath) as! ZZStatusPictureCell
        cell.backgroundColor = UIColor.whiteColor()
        
        cell.picture = pictures?[indexPath.row]
        
        return cell
    }


}

 //TODO: 设置cell模型
class ZZStatusPictureCell : UICollectionViewCell {

    //数据源
    var picture : ZZPicture? {
    
        didSet{
        let url = NSURL(string: picture?.thumbnail_pic ?? "")
        imageView.sd_setImageWithURL(url)
            
            print(url)
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.grayColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置UI界面
    private func setupUI() {
        contentView.addSubview(imageView)
        
        imageView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(contentView.snp_edges)
        }
        
    }
    
    //懒加载  闭包
    private lazy var imageView : UIImageView = {
        let iv = UIImageView()
        
        //图片的填充模式
        iv.contentMode = .ScaleAspectFill
    
        //纯代码要手动剪裁 ,xib sb会自动剪裁
        iv.clipsToBounds = true
        return iv
    }()

}






